/************************************************************************
Author:			Trey Turley, Thomas Lee, Marc Willis

Filename:		Draw.cpp

Classname:		Plotter

Description:	The plotter class allows us to plot characters
				to the screen. It uses a coordinate system to
				determine where the character will be placed.

Date Modified:	8/7/10
				- File finalized
				8/3/10
				- File created

*************************************************************************/

#include <iostream>
using namespace std;

#include "Draw.h"

/************************************************************************
 Name:			Plotter()

 Description:	Defualt Constructor

 Input:
 (None)

 Output:
 (None)
*************************************************************************/
Plotter::Plotter()
{
       coordScreen.X = 0;
       coordScreen.Y = 0;
       
       hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
       SetConsoleTextAttribute(hConsoleOutput, green);
}

/************************************************************************
 Name:			move(int x, int y)

 Description:	Moves the cursor to the specified (X,Y) location.

 Input:

 int x		-	The x coordinate on the (X,Y) grid

 int y		-	The Y coordinate on the (X,Y) grid

 Output:
 (None)
*************************************************************************/
void Plotter::move(int x, int y)
{
       coordScreen.X = x;
       coordScreen.Y = y;
       //SetConsoleTextAttribute(hConsoleOutput, c);     
       SetConsoleCursorPosition( hConsoleOutput, coordScreen );
}

/************************************************************************
 Name:			plot(int x, int y, char c)

 Description:	Plots a character on the screen at the (X,Y) location.

 Input:

 int x		-	The x coordinate on the (X,Y) grid

 int y		-	The Y coordinate on the (X,Y) grid

 char c		-	The character to be placed on the screen

 Output:
 (None)
*************************************************************************/
void Plotter::plot(int x, int y, char c)
{
       coordScreen.X = x;
       coordScreen.Y = y;
       //SetConsoleTextAttribute(hConsoleOutput, c);     
       SetConsoleCursorPosition( hConsoleOutput, coordScreen );
       cout  << c;
       cout.flush();
}

/************************************************************************
 Name:			setColor(int c)

 Description:	Allows a specific color to be set for a Plotter object.

 Input:

 int c		-	The color to be set

 Output:
 (None)
*************************************************************************/
void Plotter::setColor(int c)
{
   SetConsoleTextAttribute(hConsoleOutput, c);
}

/************************************************************************
 Name:			clear()

 Description:	Removes ALL plotted chars from the screen.

 Input:
 (None)

 Output:
 (None)
*************************************************************************/
void Plotter::clear()
{
  cls( hConsoleOutput );
}

/************************************************************************
 Name:			plotTrail(int x,int y,int color)

 Description:	Used to plot a player's trail.

 Input:
 
 int x		-	The x coordinate on the (X,Y) grid

 int y		-	The Y coordinate on the (X,Y) grid

 int color	-	The color of the trail

 Output:
 (None)
*************************************************************************/
void Plotter::plotTrail(int x,int y,int color, int dir)
{
	this->setColor(color);
	this->plot(x,y,dir);
}

/************************************************************************
 Name:			cls( HANDLE hConsole )

 Description:	Unknown...

 Input:
 
 HANDLE hConsole

 Output:
 (None)
*************************************************************************/
void Plotter::cls( HANDLE hConsole )
{
  COORD coordScreen = { 0, 0 };    // here's where we'll home the cursor

  DWORD cCharsWritten;
  CONSOLE_SCREEN_BUFFER_INFO csbi; // to get buffer info 
  DWORD dwConSize;                 // number of character cells in the current buffer 

  // get the number of character cells in the current buffer 

  GetConsoleScreenBufferInfo( hConsole, &csbi );
  dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

  // fill the entire screen with blanks 

  FillConsoleOutputCharacter( hConsole, (TCHAR) ' ', dwConSize, coordScreen, &cCharsWritten );

  // get the current text attribute 

  GetConsoleScreenBufferInfo( hConsole, &csbi );

  // now set the buffer's attributes accordingly 

  FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten );

  // put the cursor at (0, 0) 

  SetConsoleCursorPosition( hConsole, coordScreen );
 
  return;
}
